package routers

import (
	v1 "mindonmap/api/v1"
	v2 "mindonmap/api/v2"
	v3 "mindonmap/api/v3"
	v4 "mindonmap/api/v4"
	v5 "mindonmap/api/v5"
	v6 "mindonmap/api/v6"
	"mindonmap/middleware/cors"
	"mindonmap/middleware/myjwt"
	"mindonmap/pkgs/e"
	"mindonmap/pkgs/setting"
	"mindonmap/tests"

	"github.com/gin-contrib/sessions"
	"github.com/gin-contrib/sessions/cookie"
	"github.com/gin-gonic/gin"
)

func InitRouter() *gin.Engine {
	var store = cookie.NewStore([]byte("Mindonmap"))
	store.Options(sessions.Options{
		MaxAge: int(30 * 60), //30min
		Path:   "/",
	})

	r := gin.New()
	gin.SetMode(setting.RunMode)

	r.Use(gin.Logger())
	r.Use(cors.CorsHandler())
	r.Use(gin.Recovery())

	var authMiddleware = myjwt.GinJWTMiddlewareInit(&myjwt.AllUserAuthorizator{})
	var authMiddlewareLogin = myjwt.GinJWTMiddlewareLogin(&myjwt.AllUserAuthorizator{})

	r.Use(sessions.Sessions("MindonmapApi", store))
	//404 handler
	r.NoRoute(authMiddleware.MiddlewareFunc(), func(c *gin.Context) {
		code := e.PAGE_NOT_FOUND
		c.JSON(404, gin.H{"code": code, "message": e.GetMsg(code)})
	})

	// 无需鉴权
	authweb := r.Group("/v1/api")
	{
		authweb.GET("/hello", tests.Hello)
		authweb.POST("/auths/login", authMiddleware.LoginHandler)
		authweb.POST("/auths/accountlogin", authMiddlewareLogin.LoginHandler)
		authweb.POST("/auths/logincheck", v1.Logincheck)
		// Refresh time can be longer than token timeout
		authweb.GET("/auths/refresh_token", authMiddleware.RefreshHandler)
		authweb.POST("/share/info", v2.GetMindShare)
		authweb.POST("/share/restore", v2.VerifyMindShare)
		authweb.POST("/upload", v2.UploadDocument)
		authweb.GET("/image", v3.Getbase64)
		authweb.POST("/export", v3.GetPdf)
	}
	// 无需鉴权
	authpc := r.Group("/v4/api")
	{
		// 删除对象
		authpc.POST("/remove/file", v4.Removefile)
		authpc.POST("/pay/package", v4.PackageInfo)
		//下单支付
		authpc.POST("/pay/checkouts", v4.Checkouts)
		authpc.POST("/auths/login", authMiddleware.LoginHandler)
		authpc.POST("/auths/accountlogin", authMiddlewareLogin.LoginHandler)
		authpc.POST("/auths/logincheck", authMiddlewareLogin.LoginHandler)
		authpc.POST("/flow/share/info", v4.GetFlowShare)

		// Refresh time can be longer than token timeout
		authpc.GET("/auths/refresh_token", authMiddleware.RefreshHandler)
		authpc.POST("/share/info", v2.GetMindShare)
		authpc.POST("/share/restore", v4.VerifyMindShare)
		authpc.GET("/image", v3.Getbase64)
		authpc.POST("/export", v3.GetPdf)
		authpc.POST("/exports", v3.GetPdfflow)

		//设备查询
		authpc.POST("/device/list", v4.GetDeviceList)
	}

	var adminMiddleware = myjwt.GinJWTMiddlewareInit(&myjwt.AdminAuthorizator{})
	apiv1 := r.Group("/v1/api")
	//使用AdminAuthorizator中间件，只有admin权限的用户才能获取到接口
	apiv1.Use(adminMiddleware.MiddlewareFunc())
	{
		apiv1.POST("/userinfo", v1.GetUserInfo)

		apiv1.POST("/userlogout", v1.Logout)
		//获取脑图列表
		apiv1.POST("/doc/list", v1.GetMindMaps)
		//获取已删除脑图
		apiv1.POST("/doc/recycle", v1.GetMindMapsDel)
		//获取指定脑图
		apiv1.POST("/doc/info", v1.GetMindMap)
		//新建脑图
		apiv1.POST("/doc", v1.AddMindMap)
		//更新指定脑图
		apiv1.POST("/doc/save", v1.EditMindMap)
		//删除脑图
		apiv1.POST("/doc/del", v1.DeleteMindMap)
		//批量删除脑图
		apiv1.POST("/doc/dels", v1.DeleteMindMaps)

		//恢复删除
		apiv1.POST("/doc/restore", v1.RestoreMindMap)
		//彻底删除
		apiv1.POST("/doc/pulldown", v1.DelMindMap)
		//清空回收站
		apiv1.POST("/doc/cleanrecyc", v1.CleanRecycleMindMap)

		//脑图重命名
		apiv1.POST("/doc/rename", v1.RenameMind)

		//获取脑图分享列表
		apiv1.POST("/share/list", v1.GetMindShares)
		//新增脑图分享
		apiv1.POST("/share/new", v1.AddMindShare)
		//修改分享脑图
		apiv1.POST("/share/save", v1.EditMindShare)
		//取消脑图分享
		apiv1.POST("/share/del", v1.DeleteMindShare)
		//脑图分享校验

		//历史版本列表
		apiv1.POST("/ver/list", v1.GetMindVersions)
		//获取指定历史版本数据
		apiv1.POST("/ver/info", v1.GetMindVersion)
		//新增历史版本
		apiv1.POST("/ver/new", v1.AddMindVersion)
		//历史删除
		apiv1.POST("/ver/del", v1.DeleteMindVersion)

		//新增反馈
		apiv1.POST("/feedback", v1.AddFeedback)
		//新增反馈
		apiv1.POST("/feedback/info", v1.GetFeedback)

		// flow
		//新建
		apiv1.POST("/flow/increase", v3.AddMindFlow)
		//获取信息
		apiv1.POST("/flow/info", v3.GetMindFlow)
		//获取数据
		apiv1.POST("/flow/doc", v3.GetMindFlowBase)
		//更新
		apiv1.POST("/flow/save", v3.EditMindFlow)
		// 自动保存
		apiv1.POST("/flow/auto", v3.AutoSaveMindFlowData)
		//改名
		apiv1.POST("/flow/rename", v3.RenameMind)
		//删除
		apiv1.POST("/flow/del", v3.DeleteMindFlow)
		//批量删除
		apiv1.POST("/flow/dels", v3.DeleteMindFlows)
		//获取列表
		apiv1.POST("/flow/list", v3.GetMindFlows)
		//获取已删除列表
		apiv1.POST("/flow/recycle", v3.GetMindFlowsDel)
		//恢复删除
		apiv1.POST("/flow/restore", v3.RestoreMindFlow)
		//彻底删除
		apiv1.POST("/flow/pulldown", v3.DelMindFlow)
		//清空回收站
		apiv1.POST("/flow/cleanrecyc", v3.CleanRecycleMindFlow)

		//历史版本列表
		apiv1.POST("/flow/ver/list", v3.GetMindVersions)
		//获取指定历史版本数据
		apiv1.POST("/flow/ver/info", v3.GetFlowVersion)
		//新增历史版本
		apiv1.POST("/flow/ver/new", v3.AddMindVersion)
		//历史删除
		apiv1.POST("/flow/ver/del", v3.DeleteMindVersion)
		apiv1.POST("/flow/feedback", v3.AddFeedback)
		//新增脑图分享
		apiv1.POST("/flow/share/new", v3.AddFlowShare)
		//修改分享脑图
		apiv1.POST("/flow/share/save", v3.EditFlowShare)
		apiv1.POST("/flow/share/info", v3.GetFlowShare)
	}

	apipc := r.Group("/v4/api")
	//使用AdminAuthorizator中间件，只有admin权限的用户才能获取到接口
	apipc.Use(adminMiddleware.MiddlewareFunc())
	{

		//下单支付
		apipc.POST("/pay/checkout", v4.Checkout)
		apipc.POST("/pay/order/list", v4.OrderAll)             //支付结果
		apipc.POST("/pay/benefit/status", v4.BenefitStatus)    //支付权益查询
		apipc.POST("/pay/sub/status", v4.SubscriptionStatusFB) //
		apipc.POST("/pay/sub/successful", v4.BuySuccessful)    //支付成功，支付结果逻辑
		apipc.POST("/pay/sub/permission", v4.IsBuy)            //支付权益
		apipc.POST("/pay/sub/check", v4.Exportcheck)           //权益查询
		apipc.POST("/pay/file/totals", v4.MindMapTotals)       //文件数量

		authpc.POST("/upload", v4.UploadFile) // 上传图片

		//添加设备
		apipc.POST("/device/login", v4.DeviceNotFirstLogin)
		apipc.POST("/session/code", v4.GetSessionCode)

		apipc.POST("/userinfo", v4.GetUserInfo)
		apipc.POST("/userlogout", v1.Logout)
		apipc.POST("/authcode/email/passless", v4.GetCode)
		apipc.POST("/email/register", v4.Register)

		apipc.POST("/email/passless", v4.Passless_login)

		//获取脑图列表
		apipc.POST("/doc/list", v4.GetMindMaps)
		//获取已删除脑图
		apipc.POST("/doc/recycle", v4.GetMindMapsDel)
		//获取指定脑图
		apipc.POST("/doc/info", v4.GetMindMap)
		//新建脑图
		apipc.POST("/doc", v4.AddMindMap)
		apipc.POST("/copymind", v4.CopyMind)

		//更新指定脑图
		apipc.POST("/doc/save", v4.EditMindMap)
		//删除脑图
		apipc.POST("/doc/del", v4.DeleteMindMap)
		//批量删除脑图
		apipc.POST("/doc/dels", v4.DeleteMindMaps)

		//恢复删除
		apipc.POST("/doc/restore", v4.RestoreMindMap)
		//彻底删除
		apipc.POST("/doc/pulldown", v4.DelMindMap)
		//清空回收站
		apipc.POST("/doc/cleanrecyc", v4.CleanRecycleMindMap)

		//脑图重命名
		apipc.POST("/doc/rename", v4.RenameMindmap)

		//获取脑图分享列表
		apipc.POST("/share/list", v4.GetMindShares)
		//新增脑图分享
		apipc.POST("/share/new", v4.AddMindShare)
		//修改分享脑图
		apipc.POST("/share/save", v4.EditMindShare)
		//取消脑图分享
		apipc.POST("/share/del", v4.DeleteMindShare)
		//脑图分享校验

		//历史版本列表
		apipc.POST("/ver/list", v4.GetMindVersions)
		//获取指定历史版本数据
		apipc.POST("/ver/info", v4.GetMindVersion)
		//新增历史版本
		apipc.POST("/ver/new", v4.AddMindVersion)
		//历史删除
		apipc.POST("/ver/del", v4.DeleteMindVersion)

		//新增反馈
		apipc.POST("/feedback", v4.AddFeedback)
		//新增反馈
		apipc.POST("/feedback/info", v4.GetFeedback)

		// flow
		//新建
		apipc.POST("/flow/increase", v4.AddMindFlow)
		//获取信息
		apipc.POST("/flow/info", v4.GetMindFlow)
		//获取数据
		apipc.POST("/flow/doc", v4.GetMindFlowBase)
		// 复制流程图
		apipc.POST("/copyflow", v4.CopyFlow)
		//更新
		apipc.POST("/flow/save", v4.EditMindFlow)
		// 自动保存
		apipc.POST("/flow/auto", v4.AutoSaveMindFlowData)
		//改名
		apipc.POST("/flow/rename", v4.RenameMindflow)
		//删除
		apipc.POST("/flow/del", v4.DeleteMindFlow)
		//批量删除
		apipc.POST("/flow/dels", v4.DeleteMindFlows)
		//获取列表
		apipc.POST("/flow/list", v4.GetMindFlows)
		//获取已删除列表
		apipc.POST("/flow/recycle", v4.GetMindFlowsDel)
		//恢复删除
		apipc.POST("/flow/restore", v4.RestoreMindFlow)
		//彻底删除
		apipc.POST("/flow/pulldown", v4.DelMindFlow)
		//清空回收站
		apipc.POST("/flow/cleanrecyc", v4.CleanRecycleMindFlow)

		//历史版本列表
		apipc.POST("/flow/ver/list", v4.GetMindVersions)
		//获取指定历史版本数据
		apipc.POST("/flow/ver/info", v4.GetMindVersion)
		//新增历史版本
		apipc.POST("/flow/ver/new", v4.AddMindFlowVersion)
		//历史删除
		apipc.POST("/flow/ver/del", v4.DeleteMindVersion)
		apipc.POST("/flow/feedback", v4.AddFeedback)
		//新增脑图分享
		apipc.POST("/flow/share/new", v4.AddMindShare)
		//修改分享脑图
		apipc.POST("/flow/share/save", v4.EditMindShare)

	}

	//使用AdminAuthorizator中间件，只有admin权限的用户才能获取到接口
	apiv5 := r.Group("/v5/api")
	apiv5.Use(adminMiddleware.MiddlewareFunc())
	{
		//下单支付
		apiv5.POST("/pay/checkout", v4.Checkout)
		apiv5.POST("/pay/order/list", v4.OrderAll)             //支付结果
		apiv5.POST("/pay/benefit/status", v4.BenefitStatus)    //支付权益查询
		apiv5.POST("/pay/sub/status", v4.SubscriptionStatusFB) //
		apiv5.POST("/pay/sub/successful", v4.BuySuccessful)    //支付成功，支付结果逻辑
		apiv5.POST("/pay/sub/permission", v5.IsBuy)            //支付权益
		apiv5.POST("/pay/sub/check", v4.Exportcheck)           //权益查询
		apiv5.POST("/pay/file/totals", v5.MindMapTotals)       //文件数量

		apiv5.POST("/upload", v4.UploadFile) // 上传图片

		//添加设备
		apiv5.POST("/device/login", v4.DeviceNotFirstLogin)
		apiv5.POST("/session/code", v4.GetSessionCode)

		apiv5.POST("/userinfo", v4.GetUserInfo)
		apiv5.POST("/userlogout", v1.Logout)
		apiv5.POST("/authcode/email/passless", v4.GetCode)
		apiv5.POST("/email/register", v4.Register)

		apiv5.POST("/email/passless", v4.Passless_login)

		//获取脑图列表
		apiv5.POST("/doc/list", v5.GetMindMaps)
		//获取已删除脑图
		apiv5.POST("/doc/recycle", v5.GetMindMapsDel)
		//获取指定脑图
		apiv5.POST("/doc/info", v5.GetMindMap)
		//新建脑图
		apiv5.POST("/doc", v5.AddMindMap)
		apiv5.POST("/copymind", v5.CopyMind)

		//更新指定脑图
		apiv5.POST("/doc/save", v5.EditMindMap)
		//删除脑图
		apiv5.POST("/doc/del", v4.DeleteMindMap)
		//批量删除脑图
		apiv5.POST("/doc/dels", v4.DeleteMindMaps)

		//恢复删除
		apiv5.POST("/doc/restore", v4.RestoreMindMap)
		//彻底删除
		apiv5.POST("/doc/pulldown", v4.DelMindMap)
		//清空回收站
		apiv5.POST("/doc/cleanrecyc", v4.CleanRecycleMindMap)

		//脑图重命名
		apiv5.POST("/doc/rename", v4.RenameMindmap)

		//获取脑图分享列表
		apiv5.POST("/share/list", v5.GetMindShares)
		//新增脑图分享
		apiv5.POST("/share/new", v5.AddMindShare)
		//修改分享脑图
		apiv5.POST("/share/save", v5.EditMindShare)
		//取消脑图分享
		apiv5.POST("/share/del", v5.DeleteMindShare)
		//脑图分享校验

		//编辑保存脑图分享
		apiv5.POST("/share/edit/save", v5.EditSaveMindMap)
		//历史版本列表
		apiv5.POST("/ver/list", v4.GetMindVersions)
		//获取指定历史版本数据
		apiv5.POST("/ver/info", v4.GetMindVersion)
		//新增历史版本
		apiv5.POST("/ver/new", v4.AddMindVersion)
		//历史删除
		apiv5.POST("/ver/del", v4.DeleteMindVersion)

		//新增反馈
		apiv5.POST("/feedback", v4.AddFeedback)
		//新增反馈
		apiv5.POST("/feedback/info", v4.GetFeedback)

		// flow
		//新建
		apiv5.POST("/flow/increase", v4.AddMindFlow)
		//获取信息
		apiv5.POST("/flow/info", v4.GetMindFlow)
		//获取数据
		apiv5.POST("/flow/doc", v4.GetMindFlowBase)
		// 复制流程图
		apiv5.POST("/copyflow", v4.CopyFlow)
		//更新
		apiv5.POST("/flow/save", v4.EditMindFlow)
		// 自动保存
		apiv5.POST("/flow/auto", v4.AutoSaveMindFlowData)
		//改名
		apiv5.POST("/flow/rename", v4.RenameMindflow)
		//删除
		apiv5.POST("/flow/del", v4.DeleteMindFlow)
		//批量删除
		apiv5.POST("/flow/dels", v4.DeleteMindFlows)
		//获取列表
		apiv5.POST("/flow/list", v4.GetMindFlows)
		//获取已删除列表
		apiv5.POST("/flow/recycle", v5.GetMindFlowsDel)
		//恢复删除
		apiv5.POST("/flow/restore", v5.RestoreMindFlow)
		//彻底删除
		apiv5.POST("/flow/pulldown", v5.DelMindFlow)
		//清空回收站
		apiv5.POST("/flow/cleanrecyc", v5.CleanRecycleMindFlow)

		//历史版本列表
		apiv5.POST("/flow/ver/list", v4.GetMindVersions)
		//获取指定历史版本数据
		apiv5.POST("/flow/ver/info", v4.GetMindVersion)
		//新增历史版本
		apiv5.POST("/flow/ver/new", v4.AddMindFlowVersion)
		//历史删除
		apiv5.POST("/flow/ver/del", v4.DeleteMindVersion)
		apiv5.POST("/flow/feedback", v4.AddFeedback)
		//新增脑图分享
		apiv5.POST("/flow/share/new", v4.AddMindShare)
		//修改分享脑图
		apiv5.POST("/flow/share/save", v4.EditMindShare)

		// 新增模板
		apiv5.POST("/template/add", v5.AddTemplate)
		// 模板操作
		apiv5.POST("/template/operate", v5.AddTemplateOperate)
		// 我的收藏模板
		apiv5.GET("/template/collection", v5.MyCollertTemplateList)
		// 新建脑图
		apiv5.POST("/midmap/doc", v5.AddMindMap)
		//更新指定脑图
		apiv5.POST("/midmap/save", v5.EditMindMap)
		//获取指定脑图
		apiv5.POST("/midmap/info", v5.GetMindMap)
		apiv5.POST("/midmap/ai/like", v5.MindonmapLike)
		//更新脑图名称
		apiv5.POST("/midmap/rename", v5.RenameMindmap)
		//ai生成思维导图
		apiv5.POST("/ai/chat", v5.DeepThoughtHandler)

		// 更新AI生成状态
		apiv5.POST("/ai/status", v5.AiCreateStatus)
	}
	apiv5s := r.Group("/v5/api")
	{
		// 删除对象
		apiv5s.POST("/remove/file", v4.Removefile)
		apiv5s.POST("/pay/package", v5.PackageInfo)
		//下单支付
		apiv5s.POST("/pay/checkouts", v4.Checkouts)
		apiv5s.POST("/auths/login", authMiddleware.LoginHandler)
		apiv5s.POST("/auths/accountlogin", authMiddlewareLogin.LoginHandler)
		apiv5s.POST("/auths/logincheck", authMiddlewareLogin.LoginHandler)
		apiv5s.POST("/flow/share/info", v4.GetFlowShare)

		// Refresh time can be longer than token timeout
		apiv5s.GET("/auths/refresh_token", authMiddleware.RefreshHandler)
		apiv5s.POST("/share/info", v2.GetMindShare)
		apiv5s.POST("/share/restore", v4.VerifyMindShare)
		apiv5s.GET("/image", v3.Getbase64)
		apiv5s.POST("/export", v3.GetPdf)
		apiv5s.POST("/exports", v3.GetPdfflow)

		//设备查询
		apiv5s.POST("/device/list", v4.GetDeviceList)
		apiv5s.GET("/category", v5.CategoryList)
		// 模板列表
		apiv5s.GET("/template/list", v5.TemplateList)
	}

	apiv6 := r.Group("/v6/api")
	apiv6.Use(adminMiddleware.MiddlewareFunc())
	{
		apiv6.POST("/ai/chat", v6.DeepThoughtHandler)
	}
	apiv6s := r.Group("/v6/api")
	{
		apiv6s.POST("/pay/package", v6.PackageInfo)
	}
	return r
}
